package com.haochang.chunkvod.controller.adapter.order;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentCode;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.SaleUtils;
import com.haochang.chunkvod.controller.activity.order.PackageDetailsActivity;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.order.FoodDetailInfo;
import com.haochang.chunkvod.model.order.FoodInfo;
import com.haochang.chunkvod.view.vod.activity.ActivityDialogEnum;
import com.haochang.chunkvod.view.vod.activity.GoodsDetailsDialogActivity;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 * 套餐详情适配器
 */
public class PackageDetailsAdapter extends QuickAdapter
{
	private Context mContext;

	public PackageDetailsAdapter(Context context, int layoutResId, List data)
	{
		super(context, layoutResId, data);
		mContext = context;
	}

	@Override
	protected void convert(BaseAdapterHelper helper, final Object object)
	{
		final FoodInfo item = (FoodInfo) object;
		List<FoodDetailInfo> detailInfoList = item.getFoodList();
		if (CollectionUtils.isEmpty(detailInfoList))
			return;
		final LinearLayout center_div = helper.getView(R.id.lay_center_div);
		center_div.removeAllViews();
		for (final FoodDetailInfo info : detailInfoList)
		{
			final RelativeLayout viewItem = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.item_goods_detial_single, null);
			BaseTextView tv_foodName = (BaseTextView) viewItem.findViewById(R.id.tv_foodName);
			BaseTextView tv_num = (BaseTextView) viewItem.findViewById(R.id.tv_num);
			BaseTextView tv_price = (BaseTextView) viewItem.findViewById(R.id.tv_price);
			ImageView img_pencil_detail = (ImageView) viewItem.findViewById(R.id.img_pencil_detail);
			tv_foodName.setText(info.getName());
			if (!item.getType().equals("master") && info.getQuantity() > 0 && info.getQuantity() != 99999)
			{
				tv_foodName.append(Html.fromHtml("<font color=#a9a9a9>" + "(限选" + info.getQuantity() + info.getUnit() + ")" + "</font>"));
			}
			tv_price.setText(String.valueOf(SaleUtils.getSaleString(info.getPrice()) + "/" + info.getUnit()));
			if(item.getIs_present()==1)
				tv_price.setVisibility(View.INVISIBLE);
			else
				tv_price.setVisibility(View.VISIBLE);
			center_div.addView(viewItem);
			img_pencil_detail.setTag(R.id.active_food_info, item);
			img_pencil_detail.setTag(info);
			img_pencil_detail.setOnClickListener(onBaseClickListener);
			if (item.getType().equals("master"))
			{
				if (!TextUtils.isEmpty(info.getTaste_types()))
				{
					try
					{
						JSONArray array = TextUtils.isEmpty(info.getTaste_types()) ? null : new JSONArray(info.getTaste_types());
						if (array != null && array.length() > 0)
							img_pencil_detail.setVisibility(View.VISIBLE);
						else
							img_pencil_detail.setVisibility(View.INVISIBLE);
					} catch (JSONException e)
					{
						e.printStackTrace();
					}
				} else
					img_pencil_detail.setVisibility(View.INVISIBLE);
				tv_num.setText(String.valueOf("x" + info.getQuantity()));
			} else
			{
				img_pencil_detail.setVisibility(View.VISIBLE);
				if (info.getNumber() <= 0)
				{
					tv_num.setText("");
				} else
				{
					tv_num.setText(String.valueOf("x" + info.getNumber()));
				}
			}
			viewItem.setTag(info.getFood_id());
		}
		if (item.getType().equals("master"))
		{
			helper.setText(R.id.tv_title, item.getName());
		} else
		{
			helper.setText(R.id.tv_title, Html.fromHtml(item.getName() + "(已选" + "<font color=#f04e39>"
					+ ((item.getOrderNum() > 0) ? item.getOrderNum() : item.getDefaultNum()) + "</font>" + "/" + item.getQuantity() + ")"));
		}
	}

	private OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.img_pencil_detail:
				FoodInfo item = (FoodInfo) v.getTag(R.id.active_food_info);
				FoodDetailInfo info = (FoodDetailInfo) v.getTag();
				String flavor = info.getTaste_types();
				JSONArray array = null;
				try
				{
					array = TextUtils.isEmpty(flavor) ? null : new JSONArray(flavor);
				} catch (JSONException e)
				{
					e.printStackTrace();
				}
				if (array != null && array.length() > 0)
				{
					Intent intent = new Intent(mContext, GoodsDetailsDialogActivity.class);
					intent.putExtra(IntentKey.FOODINFO, info);
					if (!item.getType().equals("master"))
					{
						int orderNum = ((item.getOrderNum() > 0) ? item.getOrderNum() : item.getDefaultNum());
						int number = (item.getQuantity() - orderNum + info.getNumber()) > info.getQuantity() ? info.getQuantity()
								: (item.getQuantity() - orderNum + info.getNumber());
						intent.putExtra(IntentKey.SURPLUS_NUMBER, number);
						intent.putExtra(IntentKey.ORDERNUM, info.getNumber());
					} else
					{
						intent.putExtra(IntentKey.SURPLUS_NUMBER, info.getQuantity());
						intent.putExtra(IntentKey.ORDERNUM, info.getQuantity());
					}
					intent.putExtra(IntentKey.FLAVOR, info.getFlavor());
					intent.putExtra(IntentKey.TYPE, item.getType());
					intent.putExtra(IntentKey.ACTIVITY_ENUM, ActivityDialogEnum.FLAVOR);
					((PackageDetailsActivity) mContext).startActivityForResult(intent, IntentCode.DOODS_DETIAL_REQUEST);
				} else
				{
					Intent intent = new Intent(mContext, GoodsDetailsDialogActivity.class);
					intent.putExtra(IntentKey.FOODINFO, info);
					intent.putExtra(IntentKey.CUSTOM_DIALOG_LEFT_NAME, "数量");
					intent.putExtra(IntentKey.CUSTOM_DIALOG_CONTENT, info.getNumber() + "");
					int orderNum = ((item.getOrderNum() > 0) ? item.getOrderNum() : item.getDefaultNum());
					int number = (item.getQuantity() - orderNum + info.getNumber()) > info.getQuantity() ? info.getQuantity()
							: (item.getQuantity() - orderNum + info.getNumber());
					intent.putExtra(IntentKey.SURPLUS_NUMBER, number);
					intent.putExtra(IntentKey.ACTIVITY_ENUM, ActivityDialogEnum.DEFAULT);
					((PackageDetailsActivity) mContext).startActivityForResult(intent, IntentCode.DOODS_DETIAL_REQUEST);
				}
				break;
			default:
				break;
			}
		}
	};
}
